package com.jiawa.wiki.controller;

import com.jiawa.wiki.resp.CommonResp;
import com.jiawa.wiki.req.EbookQueryReq;
import com.jiawa.wiki.req.EbookSaveReq;
import com.jiawa.wiki.resp.EbookQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.service.EbookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 电子书 Controller
 *
 * @author 荀彧 2021/5/17
 */
@RestController
@RequestMapping(value = "/ebook")
@Slf4j
public class EbookController {

    @Resource
    private EbookService service;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResp<PageResp<EbookQueryResp>> list(@Valid @RequestBody EbookQueryReq ebookQueryReq) {
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        resp.setContent(service.list(ebookQueryReq));
        log.info("resp content is {}",resp.getContent());
        return resp;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResp save(@Valid @RequestBody EbookSaveReq saveReq) {
        CommonResp resp = new CommonResp<>();
        service.save(saveReq);
        return resp;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        service.delete(id);
        return resp;
    }
}

